package cj.web.marriage.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.constants.MemuEnum;
import cj.web.marriage.dao.SetMealDAO;
import cj.web.marriage.domain.SetMeal;

@Path("/setMeal")
public class SetMealController{

	private static Logger logger = Logger.getLogger(SetMealController.class.getName());
	
	@Autowired
	private SetMealDAO setMealDao;
	
	@Get("show")
	public String show(Model model,SetMeal setMeal) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.setmeal);
		return "/views/marriage/setMealList.vm";
	}

	@Post("getList")
	public String queryList(SetMeal setMeal) {
	
		JSONObject jo = new JSONObject();
		List<SetMeal> objs = setMealDao.query(setMeal);
		int size = setMealDao.querysize(setMeal);
		
		jo.put("data", objs);
		jo.put("draw", setMeal.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.setmeal);
		return "/views/marriage/setMealDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,SetMeal setMeal,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			setMealDao.create(setMeal);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		
		SetMeal obj = setMealDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.setmeal);
        
		return "/views/marriage/setMealEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SetMeal setMeal,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			setMealDao.update(setMeal);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		setMealDao.deleteById(id);
		return "r:show";
	}
}
